﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 剧情设计器2._0
{
    [Serializable]
    public class Story
    {
        //故事名称
        public string name = "";
        //故事简介
        public string summary = "";
        //故事中包含的节点
        public List<Node> nodes = new List<Node>();
        //世界观设定
        public List<Setting> settings = new List<Setting>();

        public override int GetHashCode()
        {
            int code = name.GetHashCode();
            code += summary.GetHashCode();
            foreach (Node node in nodes)
                code += node.GetHashCode();
            foreach (Setting setting in settings)
                code += setting.GetHashCode();
            return code;
        }

        public int getTime()
        {
            int time = 0;
            foreach(Node node in nodes)
            {
                time += node.getTime();
            }
            return time;
        }

        public void draw(Graphics g, int x, int y)
        {
            if (nodes.Count == 0) return;
            int sum_w = 0;
            Point[] pts = new Point[nodes.Count];

            int maxhot = -999999;
            int minhot = 999999;

            for (int i = 0; i < nodes.Count; i++)
            {

                if (nodes[i].hotvalue > maxhot) maxhot = nodes[i].hotvalue;
                if (nodes[i].hotvalue < minhot) minhot = nodes[i].hotvalue;
            }

            for(int i = 0; i < nodes.Count; i++)
            {
                int w = nodes[i].getW();
                pts[i].X = sum_w + x + 56;
                pts[i].Y = y - nodes[i].hotvalue * 2 + minhot - 32;
                g.DrawString(nodes[i].hotvalue.ToString(), new Font("微软雅黑", 10), new Pen(Color.Red).Brush, new PointF(pts[i].X, pts[i].Y - 24));
                nodes[i].draw(g, x + sum_w, y);
                sum_w += w;
            }

            Pen pen = new Pen(Color.LightGray);
            int mid = (maxhot + minhot) / 2;
            g.DrawLine(pen, x, y - mid + minhot - 32, sum_w + x, y - mid + minhot - 32);

            pen.Width = 2;
            for(int i = 0; i < pts.Length - 1; i++)
            {
                if (pts[i + 1].Y > pts[i].Y)
                    pen.Color = Color.Blue;
                else pen.Color = Color.Red;
                g.DrawLine(pen, pts[i], pts[i + 1]);
            }
            if (pts[pts.Length - 1].Y > pts[pts.Length - 2].Y)
                pen.Color = Color.Blue;
            else pen.Color = Color.Red;
            g.DrawLine(pen, pts[pts.Length - 1], pts[pts.Length - 2]);
        }

        public int getW()
        {
            int w = 0;
            foreach(Node node in nodes)
            {
                w += node.getW() + 2;
            }
            return w;
        }
    }
}
